package com.becca.cake.core.sms;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.becca.cake.core.constant.CommonConstants;

/**
 * 发送短信帮助类
 * @copyright  尊星(上海)信息科技有限公司
 * @author hejinguo
 * @date 2016年4月9日 下午5:22:46
 */
public class SMSHelper {
    private static final Logger logger = LoggerFactory.getLogger(SMSHelper.class);
    private static String       fomart = "UTF-8";

    public static final boolean sendMessage(SMSTemplate smsTemplate) {
        //返回值
        String result = "";
        HttpClient client = new HttpClient();
        NameValuePair[] nameValuePairs = new NameValuePair[3];
        nameValuePairs[0] = new NameValuePair("apikey", CommonConstants.YUNPIAN_APIKEY);
        nameValuePairs[1] = new NameValuePair("text", smsTemplate.msgData);
        nameValuePairs[2] = new NameValuePair("mobile", smsTemplate.phone);
        PostMethod method = new PostMethod(smsTemplate.sendUrl);
        method.setRequestBody(nameValuePairs);
        HttpMethodParams param = method.getParams();
        param.setContentCharset(fomart);
        InputStream resStream = null;
        try {
            client.executeMethod(method);
            resStream = method.getResponseBodyAsStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(resStream));
            StringBuffer resBuffer = new StringBuffer();
            String resTemp = "";
            while ((resTemp = br.readLine()) != null) {
                resBuffer.append(resTemp);
            }
            result = resBuffer.toString();
        } catch (HttpException e) {
            logger.error("发送短信http请求异常!", e);
        } catch (IOException e) {
            logger.error("发送短信IO异常!", e);
        } finally {
            try {
                if (resStream != null) {
                    resStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (StringUtils.isEmpty(result)) {
            return false;
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject.getIntValue("code") == 0) {
            return true;
        }
        return false;
    }

    public static final boolean sendMessageList(SMSTemplate smsTemplate) {
        //返回值
        String result = "";
        HttpClient client = new HttpClient();
        NameValuePair[] nameValuePairs = new NameValuePair[3];
        nameValuePairs[0] = new NameValuePair("apikey", CommonConstants.YUNPIAN_MASS_APIKEY);
        nameValuePairs[1] = new NameValuePair("text", smsTemplate.msgData);
        nameValuePairs[2] = new NameValuePair("mobile", smsTemplate.phone);
        PostMethod method = new PostMethod(smsTemplate.sendUrl);
        method.setRequestBody(nameValuePairs);
        HttpMethodParams param = method.getParams();
        param.setContentCharset(fomart);
        InputStream resStream = null;
        try {
            client.executeMethod(method);
            resStream = method.getResponseBodyAsStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(resStream));
            StringBuffer resBuffer = new StringBuffer();
            String resTemp = "";
            while ((resTemp = br.readLine()) != null) {
                resBuffer.append(resTemp);
            }
            result = resBuffer.toString();
        } catch (HttpException e) {
            logger.error("发送短信http请求异常!", e);
        } catch (IOException e) {
            logger.error("发送短信IO异常!", e);
        } finally {
            try {
                if (resStream != null) {
                    resStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (StringUtils.isEmpty(result)) {
            return false;
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject.getIntValue("total_count") > 0) {
            return true;
        } else {
            logger.error("短信发送失败,原因:" + jsonObject.toJSONString());
        }
        return false;
    }
    
    public static final boolean sendMessageMass(SMSTemplate smsTemplate) {
        //返回值
        String result = "";
        HttpClient client = new HttpClient();
        NameValuePair[] nameValuePairs = new NameValuePair[3];
        nameValuePairs[0] = new NameValuePair("apikey", CommonConstants.YUNPIAN_MASS_APIKEY);
        nameValuePairs[1] = new NameValuePair("text", smsTemplate.msgData);
        nameValuePairs[2] = new NameValuePair("mobile", smsTemplate.phone);
        PostMethod method = new PostMethod(smsTemplate.sendUrl);
        method.setRequestBody(nameValuePairs);
        HttpMethodParams param = method.getParams();
        param.setContentCharset(fomart);
        InputStream resStream = null;
        try {
            client.executeMethod(method);
            resStream = method.getResponseBodyAsStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(resStream));
            StringBuffer resBuffer = new StringBuffer();
            String resTemp = "";
            while ((resTemp = br.readLine()) != null) {
                resBuffer.append(resTemp);
            }
            result = resBuffer.toString();
        } catch (HttpException e) {
            logger.error("发送短信http请求异常!", e);
        } catch (IOException e) {
            logger.error("发送短信IO异常!", e);
        } finally {
            try {
                if (resStream != null) {
                    resStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (StringUtils.isEmpty(result)) {
            return false;
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject.getIntValue("code") == 0) {
            return true;
        }
        return false;
    }
}
